package com.xingdata.jjxc.guide;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.LoginActivity;
import com.xingdata.jjxc.activity.MainActivity;
import com.xingdata.jjxc.activity.TokenEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UserEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * 
 * @{# ViewPagerAdapter.java Create on 2013-5-2 下午11:03:39
 * 
 *     class desc: 引导页面适配器
 * 
 *     <p>
 *     Copyright: Copyright(c) 2013
 *     </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
 * 
 * 
 */
public class ViewPagerAdapter extends PagerAdapter {

	// 界面列表
	private List<View> views;
	private Activity activity;

	private static final String SHAREDPREFERENCES_NAME = "first_pref";
	
	protected ProgressDialog progressDialog;
	
	/**
	 * 【HTTP请求】声明Http请求实例，并声明参数实例
	 */
	protected HttpUtil mHttpUtil = null;
	protected HashMap<String, String> paramsMap=null;

	public ViewPagerAdapter(List<View> views, Activity activity) {
		this.views = views;
		this.activity = activity;
	}

	// 销毁arg1位置的界面
	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView(views.get(arg1));
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	// 获得当前界面数
	@Override
	public int getCount() {
		if (views != null) {
			return views.size();
		}
		return 0;
	}

	// 初始化arg1位置的界面
	@Override
	public Object instantiateItem(View arg0, int arg1) {
		((ViewPager) arg0).addView(views.get(arg1), 0);
		if (arg1 == views.size() - 1) {
			Button mStartWeiboImageButton = (Button) arg0.findViewById(R.id.start_experience);
			mStartWeiboImageButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 设置已经引导
					setGuided();
					
					//ysc20160904
//					goHome();
					UserEntity user = SP.getUserInfo(activity);
					if (user!=null) {
						if (user.getMobile().length() != 0 && user.getPw().length() != 0) {
							showProgressDialog(null,"正在登录...");
							doPost_login(user.getMobile(), user.getPw());
						} else {
							activity.startActivity(new Intent(activity, LoginActivity.class));
							activity.finish();
						}
					}
				}

			});
		}
		return views.get(arg1);
	}

	private void goHome() {
		// 跳转
		Intent intent = new Intent(activity, MainActivity.class);
		activity.startActivity(intent);
		activity.finish();
	}
	
	//ysc20160904	
	/** 1、登陆 2、登陆好了直接进界面 */
	private void doPost_login(final String mobile, final String password) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		
		paramsMap = new HashMap<String, String>();
		paramsMap.put("mobile", mobile);
		paramsMap.put("password", password);
		
		mHttpUtil.Post(App.ZZD_REQUEST_LOGIN, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity entity) {
				dismissProgressDialog();
				// SharedPreTools.putString(LoginActivity.this,
				// Common.USERNAME, mobile);
				// SharedPreTools.putString(LoginActivity.this,
				// Common.PASSWORD,Jni.Jni_encryptionStr(password,
				// mobile));
				UserEntity user = JSON.parseObject(entity.getResult(), UserEntity.class);
				// 获取Token
				TokenEntity tokenEntity = new TokenEntity();
				tokenEntity.setToken(Code.accessToken(mobile, password, user.getUserid()));
				SystemInfo.setTokenEntity(tokenEntity);
				user.setMobile(mobile);
				user.setToken(tokenEntity.getToken());
				user.setPw(password);
				SP.saveUserInfo(activity, user);
				// baocuo
				// carHudlist =
				// JUtils.getcarHudList(entity.getHudlist());
				// SP.saveHudInfo(LoginActivity.this,
				// entity.getHudlist());
				nextController();
			}

			@Override
			public void onFailure(String result) {
				showToast(result);
				dismissProgressDialog();
				activity.startActivity(new Intent(activity, LoginActivity.class));
				activity.finish();
			}
			
			@Override
			public void onLoading(int vurrent) {
				// TODO Auto-generated method stub

			}
		});
	}
	
	//ysc20160904
	/**
	 * 显示 ProgressDialog
	 */
	protected ProgressDialog showProgressDialog(String title, String message) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		if (title == null) {
			title = "提示";
		}
		progressDialog = ProgressDialog.show(activity, title, message);
		progressDialog.setCancelable(true);
		progressDialog
				.setOnCancelListener(new DialogInterface.OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						dialog.dismiss();
					}
				});
		return progressDialog;
	}
	
	//ysc20160904
	/** 显示弹窗 */
	protected void showToast(String s) {
		Toast.makeText(activity.getBaseContext(), s, Toast.LENGTH_SHORT).show();
	}
	
	//ysc20160904
	/**隐藏ProgressDialog*/
	private void dismissProgressDialog(){
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}
	
	//ysc20160904
	/**
	 * 判断网络连接状态
	 * 
	 * @param context
	 * @return 状态
	 */
	protected boolean isNetworkConnected() {
		ConnectivityManager cwjManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		cwjManager.getActiveNetworkInfo();
		boolean netSataus = true;
		if (cwjManager.getActiveNetworkInfo() == null) {

			netSataus = false;
		}
		return netSataus;
	}

	//ysc20160904
	private void nextController() {
		activity.startActivity(new Intent(activity, MainActivity.class));
		activity.finish();
	};

	/**
	 * 
	 * method desc：设置已经引导过了，下次启动不用再次引导
	 */
	private void setGuided() {
		SharedPreferences preferences = activity.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		// 存入数据
		editor.putBoolean("isFirstIn", false);
		// 提交修改
		editor.commit();
	}

	// 判断是否由对象生成界面
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return (arg0 == arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

}
